#include "../src/huffman/huffman.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, const char *argv[])
{
    int ret = 0;
    char chars[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', '\0'};
    unsigned int weight[] = {5, 7, 2, 4, 1, 9, 8};
    unsigned char length = sizeof(weight) / sizeof(int);

    /* 根据已知的权重进行编码，解码 */
    // 创建霍夫曼树
    huffmanTree *htree = fhuffmanTree.buildHTree(weight, length);
    // 打印输出霍夫曼树的关系表格
    fhuffmanTree.printHTree(htree, 2 * length);
    // 创建霍夫曼编码
    huffmanCode **hcode = fhuffmanTree.getHCode(htree, length);
    // 打印输出霍夫曼编码
    fhuffmanTree.printHCode(hcode);

    /* 根据已知的字符串进行编码 */
    char *string = (char *)"ADZFBCD";
    char *stringCode = NULL;
    fhuffmanTree.toHcode(hcode, chars, length, string, &stringCode);
    printf("The input string '%s' Encoded by Huffman is : \n\n\t%s\n\n", string, stringCode);

    /* 根据已知的字符串进行解码 */
    char *coding = (char *)"110001101110001001";
    char *codeString = NULL;
    fhuffmanTree.toString(htree, chars, length, coding, &codeString);
    printf("The input coding '%s' Decoded by Huffman is : \n\n\t%s\n\n", coding, codeString);

    fhuffmanTree.destoryHTree(htree);
    fhuffmanTree.destoryHCode(hcode);

    free(stringCode);
    stringCode = NULL;
    free(codeString);
    codeString = NULL;

    printf("system exited with return code %d\n", ret);

    return ret;
}
